home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / ddeapp / ww0117t.txt
Text File  |  1991-08-09  |  43KB  |  1,109 lines

  1. ======================================================================
  2.              WW0117: WINDOWS DYNAMIC DATA EXCHANGE (DDE)
  3. ======================================================================
  4.                                                    Revision Date: 8/91
  5. --------------------------------------------------------------------
  6. | INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY    |
  7. | ACCOMPANY THIS DOCUMENT (collectively referred to as an            |
  8. | Application Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY      |
  9. | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO    |
  10. | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A     |
  11. | PARTICULAR PURPOSE. The user assumes the entire risk as to the     |
  12. | accuracy and the use of this Application Note. This Application    |
  13. | Note may be copied and distributed subject to the following        |
  14. | conditions: 1) All text must be copied without modification and    |
  15. | all pages must be included; 2) If software is included, all files  |
  16. | on the disk(s) must be copied without modification [the MS-DOS(R)  |
  17. | utility DISKCOPY is appropriate for this purpose]; 3) All          |
  18. | components of this Application Note must be distributed together;  |
  19. | and 4) This Application Note may not be distributed for profit.    |
  20. |                                                                    |
  21. | Copyright 1991 Microsoft Corporation. All Rights Reserved.         |
  22. | Microsoft, MS-DOS, and the Microsoft logo are registered           |
  23. | trademarks and Windows is a trademark of Microsoft Corporation.    |
  24.  --------------------------------------------------------------------
  25.  
  26.  
  27.                              INTRODUCTION
  28.                              ============
  29.  
  30. The Windows dynamic data exchange (DDE) protocol is a set of
  31. guidelines that allows applications to share data freely. DDE uses
  32. either one-time data transfers or ongoing conversations in which
  33. applications send updates to one another as new data becomes
  34. available. This document introduces Windows DDE through some examples
  35. of its use and a detailed example of the operation of the protocol.
  36.  
  37.  
  38. DDE COMMUNICATIONS CAPABILITIES
  39. ===============================
  40.  
  41. It is important to understand that no changes are required to the
  42. current or previous shipping versions of Windows to use DDE. Windows
  43. allows for very flexible intertask communication. The primary means of
  44. communication is the passing of messages. Windows applications receive
  45. all of their input in the form of messages. All characters typed at
  46. the keyboard, all mouse activity, and all menu selection events are
  47. sent to the appropriate Windows applications through messages.
  48.  
  49. In Windows, applications can define private messages that have a
  50. unique meaning throughout the system. The DDE protocol defines some
  51. new messages for communication between the applications that use DDE.
  52. Windows also provides for the sharing of data between applications.
  53. The DDE protocol uses shared memory as the means of transferring data
  54. from application to application. DDE defines some structures to be
  55. used for the contents of the shared memory objects, all of which are
  56. discussed below.
  57.  
  58.  
  59. BASIC RULES
  60. ===========
  61.  
  62. DDE is a protocol that allows applications to exchange data on a real-
  63. time basis. To perform such an exchange, the two participating
  64. applications first have to engage in a DDE conversation. The
  65. application that initiates the conversation is known as the client
  66. application, and the application responding to the client is known as
  67. the server application.  A given application can be engaged in several
  68. conversations at the same time and can act as the client application
  69. in some of them and as the server application in others.
  70.  
  71. Information transferred between applications using DDE can be
  72. formatted in a number of ways. Client applications may not support all
  73. of the formats available or may prefer certain formats. In general,
  74. you do not need to be aware of the different formats, because the
  75. client and server applications automatically determine a format and
  76. use it to transfer information.
  77.  
  78. A DDE conversation between two applications actually takes place
  79. between two windows, one for each of the participating applications.
  80. An application opens a window for each conversation it engages in
  81. (note that this window is not typically visible).
  82.  
  83. DDE uses the three-level hierarchy -- application, topic, and item --
  84. to uniquely identify a unit of data. Application is the name of the
  85. DDE server. Item is a data object that can be passed in a DDE data
  86. exchange. Topic is a logical data context. For applications that
  87. operate on file-based documents, topics are usually filenames; for
  88. other applications, they are other application-specific strings. A
  89. syntax example is
  90.  
  91.   =Signal|NYSE!IBM
  92.  
  93. which a spreadsheet would read as follows:
  94.  
  95.   =Application|Topic!Item
  96.  
  97. This indicates that the cell content is to be the value of the data
  98. item "IBM" in the topic "NYSE" of the application "Signal".
  99.  
  100. After a conversation has been started, the client application
  101. interacts with the server by issuing transactions. When issuing a
  102. transaction, the client asks the server to preform a given action.
  103. There are six types of transactions: Request, Advise, Unadvise, Poke,
  104. Execute, and Terminate. These are permitted only within an initiated
  105. conversation. DDE conversations are one-way: the client application is
  106. always the one that issues the transactions. If the server wants to
  107. issue a transaction to the client, the server is expected to initiate
  108. a new conversation for that purpose. The server becomes the client in
  109. this new conversation. (The only exception to the one-way rule is the
  110. Terminate transaction, which can be issued by either the client or the
  111. server.)
  112.  
  113.  
  114. DDE CONCEPTS
  115. ============
  116.  
  117. All DDE communication between applications occurs on a channel. The
  118. DDE Initiate function is used to open a channel, and the Terminate
  119. function is used to close a channel.
  120.  
  121. The application that initiates a channel is called the client, and the
  122. other application is called the server. The client controls the
  123. channel and requests services from the server.
  124.  
  125. The three functions used by the client on an open DDE channel are
  126. Request, Poke, and Execute. Request gets data from the server, Poke
  127. sends data to the server, and Execute sends commands to the server.
  128.  
  129. Initiate
  130. --------
  131.  
  132. The Initiate function opens a DDE channel from a client to a server
  133. application. The Initiate function has two parameters, the server
  134. application name and the topic. The application name is the server
  135. program name without the .EXE extension. If the server application is
  136. not running, the Initiate function automatically starts it. (Initiate
  137. may not automatically start the server from some applications; this is
  138. a Microsoft convention.)
  139.  
  140. The topic identifies something in the server application that you plan
  141. to access. The topic is often the name of a window in the server
  142. application.
  143.  
  144. Initiate returns a channel number. The channel number is subsequently
  145. used as a parameter to all other DDE functions to identify the channel
  146. in use. You may have more than one channel open by calling the
  147. Initiate function several times.
  148.  
  149. An error is returned if the server application is not running and
  150. cannot be started, or if the topic is not valid.
  151.  
  152. Request
  153. -------
  154.  
  155. The Request function gets data from the server application. The
  156. parameters are the channel number and the item. The channel number is
  157. the value returned by Initiate. The item identifies the data to be
  158. returned.
  159.  
  160. The server application returns the desired data. An error is returned
  161. if the channel number is not valid, the item is not valid, or the
  162. server cannot return the data.
  163.  
  164. Poke
  165. ----
  166.  
  167. The Poke function sends data to the server application. The parameters
  168. are the channel number, the item, and the data. The channel number is
  169. the value returned by Initiate. The item identifies the type of data
  170. being sent. The data is the actual data to be sent to the server.
  171.  
  172. An error is returned if the channel number is not valid, the item is
  173. not valid, or the server is not able to accept the data.
  174.  
  175. Execute
  176. -------
  177.  
  178. The Execute function sends commands to the server application. The
  179. parameters are the channel number and the execute string. The channel
  180. number is the value returned by Initiate. The execute string contains
  181. one or more commands to be executed by the server.
  182.  
  183. Different server applications support different commands. In general,
  184. the commands that can be sent to an application are the commands in
  185. that application's menus. An error is returned if the channel number
  186. is not valid, or if any errors occur when the server executes the
  187. commands in the execute string. Execute does not return data to the
  188. client.
  189.  
  190. Terminate
  191. ---------
  192.  
  193. The Terminate function closes a DDE channel. The parameter is the
  194. channel number of the DDE channel to close. An error is returned if
  195. the channel number is not valid.
  196.  
  197.  
  198. TERMINOLOGY
  199. ===========
  200.  
  201. Dynamic Data Exchange (DDE)
  202. ---------------------------
  203.  
  204. A Windows protocol that allows two Windows applications to communicate
  205. with each other, allowing for the continuous and automatic exchange of
  206. data without user intervention.
  207.  
  208. Conversation
  209. ------------
  210.  
  211. Two Windows applications using DDE to exchange data. The conversation
  212. is conducted through a channel. DDE client is the application that
  213. initiated the conversation; DDE server is the application that
  214. responds to the DDE client.
  215.  
  216. Task Identification Number
  217. --------------------------
  218.  
  219. A unique number that identifies a specific copy of an application when
  220. several copies are running at the same time. The task ID is appended
  221. to the application name to identify the application (for example,
  222. Excel4321).
  223.  
  224. Item
  225. ----
  226.  
  227. A reference to a piece of data (such as an integer, a string, a range
  228. of cells in a worksheet, a chart, or a bitmap) that can be passed
  229. between two applications engaged in a DDE conversation.
  230.  
  231. Topic
  232. -----
  233.  
  234. Information that defines the "subject" of a DDE conversation and
  235. represents some unit of data that is meaningful to the DDE server
  236. conversation. For most applications that operate on files, this is a
  237. filename (for example, SALES.XLS).
  238.  
  239.  
  240. USES FOR WINDOWS DYNAMIC DATA EXCHANGE
  241. ======================================
  242.  
  243. The uses for DDE mentioned in this section are intended simply as a
  244. starting point. There are many possibilities for the use of DDE, and
  245. here we can suggest only a few. These are only ideas for the use of
  246. DDE; they do not represent a commitment on the part of Microsoft to
  247. provide these features in any application.
  248.  
  249. A sample Excel spreadsheet has the following layout:
  250.  
  251.           STOCK    SHARES     PRICE    EXTENSION
  252.      1    MSFT       3500        78       155000
  253.      2    LOTS        500        25         4000
  254.      3    TATE       2000        35         7000
  255.      4    IBM        1000       148        80000
  256.  
  257. Without DDE, you could update this spreadsheet using the Clipboard to
  258. manually copy numbers from the stock quote application into the Excel
  259. spreadsheet. This method requires switching between applications and
  260. requires that you pay attention to the price data and undertake the
  261. data exchange when desired.
  262.  
  263. With DDE, this system is much more automatic, providing the
  264. spreadsheet with the current values for multiple data items without
  265. your intervention. DDE allows you to set up a conversation between the
  266. server and client applications that keeps the spreadsheet informed of
  267. any changes in the value of the stocks that it has asked about.
  268.  
  269. Once this connection is established, the cell value will always
  270. reflect the most current data available from the server. No per-
  271. transfer intervention on your part is necessary. This facilitates the
  272. analysis of real-time data in a timely manner.
  273.  
  274. The usefulness of DDE is not restricted to specialized real-time data
  275. acquisition applications. Productivity software in general can benefit
  276. significantly from the protocol. For example, suppose you want to
  277. prepare a report document monthly using a graphics and text word
  278. processor. The report is to include graphics generated in a separate
  279. business graphics package. Without DDE, you must perform a manual
  280. copy-and-paste process to include each month's new graphs in each
  281. month's report document. With DDE, the word processor can establish a
  282. permanent link to the charting application so that any changes you
  283. make to the charting document are reflected in the word processing
  284. document, either automatically or on request. This makes the routine
  285. of document preparation much simpler.
  286.  
  287.  
  288.                             DDE AND EXCEL
  289.                             =============
  290.  
  291. HOW TO USE THE EXCEL DDE MACRO COMMANDS
  292. =======================================
  293.  
  294. The examples below all execute another copy of Excel. Any application
  295. that supports the DDE protocol could be substituted for Excel. To
  296. understand the information presented, you should be familiar with the
  297. following (see the "Microsoft Excel Functions and Macros" manual for
  298. further reference):
  299.  
  300.    Constructing a command macro
  301.    The EXEC macro command
  302.    The EXECUTE macro command
  303.    The INDEX macro command
  304.    The INITIATE macro command
  305.    The POKE macro command
  306.    The REQUEST macro command
  307.    The TERMINATE macro command
  308.  
  309. Using the INITIATE Command
  310. --------------------------
  311.  
  312. The syntax is as follows:
  313.   
  314.    =INITIATE(<app_text>,<topic_text>)
  315.     
  316.       <App_text> is the DDE name of the application you are accessing.
  317.       <Topic_text> is the item in the application you are accessing.
  318.     
  319. The sample macro below executes another copy of Excel and initiates
  320. two DDE channels to that copy of Excel. The macro is as follows:
  321.  
  322.    A2 =INITIATE("Excel","system")
  323.    A3 =INITIATE("Excel","Sheet1")
  324.    A4 =TERMINATE(A2)
  325.    A5 =TERMINATE(A3)
  326.    A6 =RETURN()
  327.  
  328. After this macro is executed, no values will have been placed in new
  329. locations, but you will have successfully opened and closed two DDE
  330. channels.
  331.  
  332. Please note the following:
  333.  
  334. 1. Each time the INITIATE command is executed, a new DDE channel is
  335.    opened. Each channel is assigned a unique number starting at 0
  336.    (zero). The cells containing the two INITIATE commands will contain
  337.    0 in the first cell and 1 in the second cell as long as another
  338.    channel from another INITIATE was not left open. If another channel
  339.    was left open, the cells will contain the next increment of the
  340.    channel number.
  341.  
  342. 2. In the above example, DDE channels are initiated with the topics of
  343.    "system" and "Sheet1". Any Excel sheet name is a valid topic for
  344.    use in an INITIATE command. The topic of "system" allows you to ask
  345.    Excel for specific system information. The topics are either
  346.    "system" or the names of current documents.
  347.  
  348. Using the REQUEST Command
  349. -------------------------
  350.  
  351. The sample macro below executes another copy of Excel and requests a
  352. value from the worksheet. The following assumptions are made in this
  353. example:
  354.  
  355. 1. A second copy of Excel is being used as the second application for
  356.    the DDE link.
  357. 2. The value being requested is contained in cell A1 of AMORTIZE.XLS.
  358. 3. Your EXCEL directory is the current directory.
  359.  
  360. The macro is as follows:
  361.  
  362.    A3 Comment: DDE request from Excel
  363.    A4 =EXEC("excel library\amortize.xls",2)
  364.    A5 chan=INITIATE("Excel"&A4,"amortize.xls")
  365.    A6 =REQUEST(chan,"R1C1")
  366.    A7 =TERMINATE(chan)
  367.    A8 =RETURN()
  368.  
  369. After this macro is executed, the value in cell A1 of AMORTIZE.XLS can
  370. be found on the macro sheet in the cell containing the REQUEST
  371. command; in this example, that cell is A6.
  372.  
  373. When you use the REQUEST command to request data from Excel, any
  374. referencing made must be in R1C1-type format. If this format is not
  375. used, the REQUEST command returns a #REF error.
  376.  
  377. Using the REQUEST Command for System Information
  378. ------------------------------------------------
  379.  
  380. The following example explains how to use the REQUEST command from an
  381. Excel macro with the topic of "system". The macro executes another
  382. copy of Excel, initiates a DDE channel to that copy of Excel with the
  383. topic of "system", and uses the REQUEST command for system
  384. information. This example assumes that a second copy of Excel is being
  385. used as the second application for the DDE link.
  386.  
  387. The macro is as follows:
  388.  
  389.    A3 =INITIATE("Excel","system")
  390.    A4 =INDEX(REQUEST(A3,"SysItems"),3)
  391.    A5 =TERMINATE(A3)
  392.    A6 =RETURN()
  393.  
  394. The request will return an array of the items supported on the topic
  395. "system". You can use the INDEX function to access each element of the
  396. array of items that is returned.
  397.  
  398. Please note that the system topic "SysItems" returns a list of
  399. available "system" items. Any item returned by "SysItems" can be used
  400. as a "system" item and, thus, can be used in the REQUEST function. For
  401. Excel, the list of available items includes the following:
  402.  
  403.    SysItems
  404.    Topics
  405.    Status
  406.    Formats
  407.    Selection
  408.  
  409. Any of these items is legal for use with the "system" topic in Excel.
  410. In the previous example, you could replace your request for "SysItems"
  411. with one of the items returned -- for example, "formats":
  412.  
  413.    A18 Comment: Getting SysItems from Excel
  414.    A19 =EXEC("excel",2)
  415.    A20 chan=INITIATE("Excel"&A19,"System")
  416.    A21 =REQUEST(chan,"formats")
  417.    A22 =TERMINATE(chan)
  418.    A23 =RETURN()
  419.  
  420. To make this macro request the formats from Word for Windows instead,
  421. do the following:
  422.  
  423. 1. Delete Cell A19.
  424. 2. Change cell A20 to:
  425.  
  426.       chan=INITIATE("WinWord","System")
  427.  
  428. You can use the INDEX function to look up values returned by REQUEST.
  429. However, if more than one value is to be returned by REQUEST, you can
  430. use the FORMULA.ARRAY function in place of INDEX to return all of the
  431. values at once.
  432.  
  433. Using the POKE Command
  434. ----------------------
  435.  
  436. This example explains how to use the POKE command from an Excel macro.
  437. The sample macro below executes another copy of Excel and places a
  438. value into a worksheet. In the following example:
  439.  
  440. 1. A second copy of Excel is being used as the second application for
  441.    the DDE link.
  442. 2. The text to be sent over the DDE channel is contained on the macro
  443.    sheet in cell A39.
  444. 3. The text will be placed into Sheet1 in the second copy of Excel.
  445.  
  446. The macro is as follows:
  447.  
  448.    A39 Comment: DDE Poke to another Excel
  449.    A40 =EXEC("excel",2)
  450.    A41 chan=INITIATE("Excel"&A40,"Sheet1")
  451.    A42 =POKE(chan,"R1C1",A39)
  452.    A43 =TERMINATE(chan)
  453.    A44 =RETURN()
  454.  
  455. After this macro is executed, the text that is in cell A39 of the
  456. macro sheet will have been placed in cell A1 of Sheet1 in the second
  457. copy of Excel.
  458.  
  459. When using the POKE command to place a value in another copy of Excel,
  460. any references made to the destination sheet must be in R1C1-type
  461. format. If this format is not used, the POKE statement returns a #REF
  462. error.
  463.  
  464.  
  465. Using the EXECUTE Command
  466. -------------------------
  467.  
  468. The following macro runs another copy of Excel and makes the other
  469. copy of Excel load the file AMORTIZE.XLS from the LIBRARY directory.
  470. Note the use of the defined name {q} for quotation marks; this is
  471. often a useful technique when sending execute strings that contain
  472. quotation marks.
  473.  
  474.    A54 Comment: DDE Execute makes another instance of Excel load
  475.        AMORTIZE.XLS
  476.    A55 =EXEC("excel",2)
  477.    A56 chan=INITIATE("Excel"&A55,"Sheet1")
  478.    A57 q=CHAR(34)
  479.    A58 =EXECUTE(chan,"[open("&q&"library\amortize.xls"&q&")]")
  480.    A59 =TERMINATE(chan)
  481.    A60 =RETURN()
  482.  
  483. Appending a Windows Task ID
  484. ---------------------------
  485.  
  486. The EXEC function returns a task ID number for whatever program it
  487. starts. This task ID number can be appended to the <app_text> field of
  488. the INITIATE function for establishing a DDE channel to the specific
  489. instance of the program started by EXEC.
  490.  
  491. However, if the topic of your INITIATE function is already opened and
  492. the Ignore Remote Requests box under the Options Workspace menu is
  493. selected, you will get the error message
  494.  
  495.    Remote data not accessible, start application <APPLICATION.EXE>?
  496.  
  497. where <APPLICATION.EXE> is truncated/expanded to eight characters, as
  498. with any DOS filename. If the filename contains fewer than eight
  499. characters, then as many of the task ID numbers as will fit to make up
  500. eight characters are appended and displayed. Clearing the Ignore
  501. Remote Requests option corrects the problem. This can be done by
  502. choosing Workspace from the Options menu and manually clearing Ignore
  503. Remote Requests, or by using the WORKSPACE function.
  504.  
  505. Excel Format Information
  506. ------------------------
  507.  
  508. Valid Excel 3.0 DDE formats are the following:
  509.    
  510.    XLTable
  511.    BIFF3
  512.    SYLK
  513.    WK1
  514.    CSV
  515.    Text
  516.    Rich Text Format
  517.    DIF
  518.    BITMAP
  519.    METAFILEPICT
  520.    Printer_Picture
  521.  
  522. Excel cycles through these formats with each request for data until it
  523. receives an acknowledgment from the server. Thus, if a server can
  524. supply data only in Text, Excel cycles through all six formats each
  525. time it requests data from the server.
  526.  
  527. Speed Issues and DDE
  528. --------------------
  529.  
  530. When Excel receives DDE messages, the messages are posted on the
  531. application's message queue along with all other Windows messages.
  532. Before Excel processes DDE messages, the messages are taken from the
  533. application's queue and stored in a DDE queue. This queue is limited
  534. to eight items at a given time.
  535.  
  536. The DDE messages are then processed in tandem with Windows messages on
  537. a priority basis (certain Windows messages having greater priority
  538. than the DDE messages). This accounts for the speed problems that
  539. occur when DDE messages are passed to Excel.
  540.  
  541. Since the DDE queue is fixed at eight items, some items may be lost if
  542. the transmission speed of the DDE messages is increased. At this time,
  543. the DDE message queue's size cannot be increased. The application
  544. sending the DDE data must slow down the rate at which it sends Excel
  545. DDE messages for Excel to process them.
  546.  
  547. Sending data to Excel using DDE is fastest if Excel's XLTable format
  548. is used. If another format such as Text or CSV is used, Excel must
  549. take the time to parse and error check the data instead of accepting
  550. it directly.
  551.  
  552. The limit for consecutive messages for the task (application) queue is
  553. eight. There is no inherent limit set on the number of DDE links that
  554. you can establish within a document. The number is limited only by the
  555. amount of available memory.
  556.  
  557. Using the ON.DATA Command in Excel
  558. ----------------------------------
  559.  
  560. Use the ON.DATA command in Excel when you want a particular macro to
  561. run any time there is any type of update to a particular worksheet via
  562. DDE. The syntax of the formula is:
  563.  
  564.    =ON.DATA("<document_text>","<macro_text>")
  565.   
  566. If executed properly, the macro defined by <macro_text> will run every
  567. time there is a DDE update to the worksheet specified by
  568. <document_text>.
  569.  
  570. When using the ON.DATA command in Excel, you must verify the
  571. following:
  572.  
  573. 1. Ignore Remote Requests is not selected in Excel (from the Options
  574.    menu, choose Workspace). You can ensure that Ignore Remote Requests
  575.    is not selected by issuing the following macro statement before the
  576.    ON.DATA command:
  577.  
  578.       =WORKSPACE(,,,,,,,false)
  579.  
  580. 2. The argument for <document_text> in the ON.DATA function must refer
  581.    to a sheet name that contains remote references and only the sheet
  582.    name, not including cell references.
  583.  
  584.    Valid ON.DATA commands look like the following:
  585.  
  586.       =ON.DATA("Sheet1.xls","Macro1.xlm!R1C1")
  587.       =ON.DATA("Sheet1.xls","Macro1.xlm!Macro_name")
  588.  
  589.    You must not reference any particular cells. The following would
  590.    not be valid:
  591.  
  592.       =ON.DATA("Sheet1.xls!$A$1","Macro1.xlm!R1C1")
  593.       =ON.DATA("Sheet1.xls!Area1","Macro1.xlm!Macro_name")
  594.  
  595. Using the Topic "System" to Obtain Word for Windows System Information
  596. ----------------------------------------------------------------------
  597.  
  598. To request system information from Word for Windows in an Excel macro,
  599. use the topic "system" when initiating a DDE channel. Once a channel
  600. has been opened with the topic "system", Word for Windows recognizes
  601. the topics SysItems, Topics, and Formats.
  602.  
  603.    Item         Effect
  604.    ----         ------
  605.    
  606.    SysItems     Returns a list of all items you can use with the
  607.                 "system" topic
  608.    Topics       Returns a list of currently open documents, including
  609.                 full paths
  610.    Formats      Returns a list of all the Clipboard formats supported
  611.                 by Word for Windows
  612.  
  613. The Excel macro below does the following:
  614.  
  615. 1. Initiates a DDE channel using the topic "system"
  616. 2. Uses the topic "Formats" to get the first three formats supported
  617.    by Word for Windows
  618. 3. Places the list into cells A10:A12
  619.  
  620. The macro is as follows:
  621.  
  622.    A1     Comment: DDE_Example
  623.    A2     =INITIATE("Winword","system")
  624.    A3     =INDEX(REQUEST(A2,"Formats"),1)
  625.    A4     =FORMULA(A3,A10)
  626.    A5     =INDEX(REQUEST(A2,"Formats"),2)
  627.    A6     =FORMULA(A5,A11)
  628.    A7     =INDEX(REQUEST(A2,"Formats"),3)
  629.    A8     =FORMULA(A7,A12)
  630.    A9     =RETURN()
  631.    A10    Rich Text Format
  632.    A11    TEXT
  633.    A12    METAFILEPICT
  634.    
  635.  
  636.                        DDE AND WORD FOR WINDOWS
  637.                        ========================
  638.  
  639. Word for Windows uses two different types of DDE commands: WordBasic
  640. commands, which are designed for complex or custom DDE conversations,
  641. and DDE fields, intended for simple links updating information from
  642. supporting documents, either manually or automatically.
  643.  
  644.  
  645. MACRO COMMANDS
  646. ==============
  647.  
  648. When using DDE with Word for Windows as the client you should use the
  649. Windows Dynamic Link Library GetModuleHandle to determine if your
  650. server application is loaded or not (see page 25 in the "Microsoft
  651. Word for Windows Technical Reference). If it is not loaded, issue a
  652. Shell command to load it. You may also specify the document that will
  653. be loaded.
  654.  
  655. In the example below, a user defined function IsAppLoaded is declared
  656. (see page 19 in the "Microsoft Word for Windows Technical Reference;"
  657. or pages 70-71 of the "Microsoft Word for Windows and OS/2 Technical
  658. Reference"). IsAppLoaded is a library function in the Windows kernel
  659. listed as GetModuleHandle. IsAppLoaded takes a single string parameter
  660. that is the name of the application you are checking on -- in this
  661. case, Server. If Server is already in memory, IsAppLoaded is true,
  662. returning a non-zero value.
  663.  
  664. In the following routine, the text between <> should appear as one
  665. line without the <> symbol:
  666.  
  667.    <Declare Function IsAppLoaded Lib "kernel" (name$) As Integer Alias
  668.    "GetModuleHandle">
  669.    Sub Main
  670.      If IsAppLoaded("Server") = 0 then Shell "server"
  671.      chan = DDEInitiate("server", "stockinfo")
  672.      DDETerminate chan
  673.    End Sub
  674.    
  675. Note: The term "Server" is used here in a general sense. It could be
  676. replaced with any Windows application's DDE name.
  677.  
  678. If Server is not in memory, the Shell statement will load it. The
  679. second parameter indicates Server should be loaded as an icon. It is
  680. also possible to specify a filename to be loaded when an application
  681. is Shelled. The syntax is
  682.   
  683.    Shell "<servername> <path\filename>", 2
  684.  
  685. where <servername> is the name of the network server you want to
  686. access, and path\filename> is the path to and name of the application
  687. to start.
  688.  
  689. By using the IsAppLoaded function to determine if the server
  690. application needs to be loaded or not, you can avoid the message
  691. "Remote data (<filename>) not accessible: Start application <appname>
  692. ? Y/N ? ". This reduces the need for operator intervention.
  693.  
  694. We recommend that you use the following code syntax when establishing
  695. DDE communication, replacing the references to Excel and ANNUAL.XLS
  696. with the appropriate server name and topic.
  697.  
  698. In the following routine, the text between <> should appear as one
  699. line without the <> symbol:
  700.  
  701.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  702.    "GetModuleHandle">
  703.    Sub Main
  704.      If IsAppLoaded("excel") = 0 Then Shell "excel c:\annual.xls"
  705.      chan1 = DDEInitiate("excel", "annual.xls")
  706.      DDETerminateAll
  707.    End Sub
  708.    
  709.  
  710. Each time a DDEInitiate statement is executed, a new instance of the
  711. server will be loaded. For applications permitting only a single
  712. instance to be loaded this method will generate errors. To overcome
  713. this problem, use the topic "system" when establishing the DDE
  714. channel. This will establish the channel to the application's main
  715. window instead of to a specific file (<topic>). Then use the
  716. DDEExecute commands to open and close individual files as needed.
  717.    
  718. In the following routine, the text between <> should appear as one
  719. line without the <> symbol:
  720.  
  721.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  722.    "GetModuleHandle">
  723.    Sub Main
  724.      If IsAppLoaded("server") = 0 Then Shell "server"
  725.      chan1 = DDEInitiate("server", "system")
  726.      DDETerminateAll
  727.    End Sub
  728.    
  729. Use of the topic "system" with Word for Windows allows access to three
  730. special items -- SysItems, Topics, and Formats.
  731.  
  732.    Item       Effect
  733.    ----       ------
  734.    
  735.    SysItems   Returns a list of all items you can use with the
  736.               "system" topic
  737.    Topics     Returns a list of currently open documents, including
  738.               full paths
  739.    Formats    Returns a list of all the Clipboard formats supported by
  740.               Word for Windows
  741.  
  742. Word returns these values in CF_Text format (ASCII text). Items in a
  743. list are separated by tabs.
  744.  
  745.  
  746. COMMAND SYNTAX
  747. ==============
  748.  
  749. DDEInitiate
  750. -----------
  751.  
  752. When Word is the client application, DDEInitiate is used to attempt to
  753. start a conversation with another application that will be the server.
  754. If the command is successful, a channel number, which will be greater
  755. than 1, is returned. The number of channels that can be opened is
  756. limited by memory.
  757.  
  758. Syntax:
  759.    
  760.    DDEInitiate(<App$>, <Topic$>)
  761.  
  762. where <App$> is a text string indicating the DDE name of the
  763. application that will be the server. <Topic$> will normally be the
  764. name of a document or file, but this is application specific.
  765.  
  766. When the DDEInitiate command is executed, a DDE channel is opened to
  767. the specified application and file. The DDE channel numbers begin with
  768. 1 and increment as each channel is opened. When a channel is
  769. terminated, that channel number will be reused for the next
  770. DDEInitiate.
  771.  
  772. In normal usage, the return value -- the channel number -- is assigned
  773. to a variable. Referencing the variable is preferable to using the
  774. actual channel number. For example:
  775.    
  776.    chan = DDEInitiate ("excel","budget.xls")
  777.  
  778. If Excel and BUDGET.XLS are found and started, the returned channel
  779. number is assigned to the variable chan. Then, you would use chan when
  780. referring to this channel.
  781.  
  782. If the <app$> or <topic$> is not found, an error is generated. Using
  783. the WordBasic On Error statements, you can trap these errors and
  784. institute error routines to allow the user to respecify the <app$> or
  785. <topic$> so your macro can continue.
  786.  
  787. In the example below, the application DDE name for Excel is assigned
  788. to the string variable App1$, and the name of the worksheet wanted,
  789. BUDGET.XLS, to Topic1$. Then, the DDEInitiate command is issued, the
  790. variables App1$ and Topic1$ are used.
  791.    
  792. In the following routine, the text between <> should appear as one
  793. line without the <> symbol:
  794.  
  795.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  796.    "GetModuleHandle">
  797.    Sub Main
  798.      If IsAppLoaded("Excel") = 0 Then Shell "excel"
  799.      App1$ = "excel"
  800.      Topic1$ = "budget.xls"
  801.      chan = DDEInitiate (App1$, Topic1$)
  802.    End Sub
  803.  
  804. DDE Terminate
  805. -------------
  806.  
  807. Syntax:
  808.  
  809.    DDETerminate <ChanNum>
  810.  
  811. DDETerminate closes a DDE channel after all communication is
  812. completed. It is useful when multiple conversations are going on and
  813. you want to end one while maintaining the others. By assigning a
  814. variable to the returned channel number, you can selectively end
  815. individual DDE conversations.
  816.  
  817. In the example below, DDE channel chan2 is opened and selectively
  818. closed, and another server is opened while maintaining the DDE channel
  819. chan1. Finally, both channels are closed by issuing individual
  820. DDETerminate statements.
  821.  
  822. In the following routine, the text between <> should appear as one
  823. line without the <> symbol:
  824.    
  825.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  826.    "GetModuleHandle">
  827.    Sub Main
  828.      If IsAppLoaded("excel") = 0 Then Shell "excel"
  829.      chan1 = DDEInitiate ("excel", "budget.xls")
  830.      If IsAppLoaded("server") = 0 Then Shell "server"
  831.      chan2 = DDEInitiate ("server", "stockinfo")
  832.      DDETerminate chan2
  833.      If IsAppLoaded("SB4W") = 0 Then Shell "SB4W"
  834.      chan2 = DDEInitiate ("SB4W", "system")
  835.      DDETerminate chan2
  836.      DDETerminate chan1
  837.    End Sub
  838.  
  839. DDETerminateAll
  840. ---------------
  841.  
  842. Syntax:
  843.  
  844.    DDETerminateAll
  845.  
  846. DDETerminateAll terminates all DDE channels in which Word for Windows
  847. is a client. It can be used as a general cleanup to ensure that all
  848. channels have been closed without you having to know which channels
  849. are open.
  850.  
  851. In the example below, two independent DDE conversations, chan1 and
  852. chan2, are started, and DDETerminateAll closes both sessions.
  853.  
  854. In the following routine, the text between <> should appear as one
  855. line without the <> symbol:
  856.    
  857.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  858.    "GetModuleHandle">
  859.    Sub Main
  860.      If IsAppLoaded("excel") = 0 Then Shell "excel"
  861.      chan1 = DDEInitiate ("excel", "budget.xls")
  862.      If IsAppLoaded("server") = 0 Then Shell "server"
  863.      chan2 = DDEInitiate ("server", "stockinfo")
  864.      DDETerminateAll
  865.    End Sub
  866.  
  867. DDERequest$
  868. -----------
  869.  
  870. Syntax:
  871.     
  872.    data$ = DDERequest$ (<ChanNum>, <Item$>)
  873.  
  874. DDERequest$ requests specific information from the server on <Item$>
  875. using the DDE channel designated. The channel must have previously
  876. been opened using DDEInitiate. Because data is returned in CF_TEXT
  877. format, it can only be assigned to a string variable. If a numeric
  878. value is needed, use the Val statement to convert the text string to a
  879. numeric. Pictures or text in rich-text format cannot be transferred
  880. using DDERequest$. If DDERequest$ is unsuccessful, a null string ("")
  881. is returned.
  882.  
  883. In the example below, a DDE channel to Excel and the worksheet
  884. BUGDET.XLS is opened and assigned to chan. A request for information
  885. on the item IBM is sent. If successful, the current value of the
  886. defined name balance is printed on the status line; otherwise, a null
  887. string is returned, and nothing is displayed on the status line.
  888.  
  889. In the following routine, the text between <> should appear as one
  890. line without the <> symbol:
  891.    
  892.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  893.    "GetModuleHandle">
  894.    Sub Main
  895.      If IsAppLoaded("server") = 0 Then Shell "server"
  896.      chan = DDEInitiate ("server", "stockinfo")
  897.      data$ = DDERequest$ (chan, "ibm")
  898.      Print data$
  899.      DDETerminate chan
  900.    End Sub
  901.  
  902. In the example below, a channel is opened and information from the
  903. <item$> balance is requested. If the reference "balance" is not
  904. defined in the document BUDGET.XLS, a null string is received.
  905.    
  906. In the following routine, the text between <> should appear as one
  907. line without the <> symbol:
  908.    
  909.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  910.    "GetModuleHandle">
  911.    Sub Main
  912.      chan = DDEInitiate ("excel", "budget.xls")
  913.      data$ = DDERequest$ (chan, "balance")
  914.      Print data$
  915.      DDETerminate chan
  916.    End Sub
  917.  
  918. DDEPoke
  919. -------
  920.  
  921. Syntax:
  922.   
  923.    DDEPoke <ChanNum>, <Item$>, <Data$>
  924.  
  925. DDEPoke is used by a client to send information to the server. The
  926. string variable <Data$> is sent to the location, <Item$>, using
  927. <ChanNum>. If the <Item$> does not exist, a WordBasic error is
  928. generated.
  929.  
  930. In the example below, after establishing a DDE channel with Server, a
  931. request is made for the <Item$> TOTAL, which is a named reference in
  932. BUDGET.XLS. If the numeric value of the string returned is 0, the
  933. amount $100.00 is POKEd into the cell referenced by TOTAL. If the
  934. named reference TOTAL does not exist in BUDGET.XLS, a WordBasic error
  935. 503, "Process failed in other application," is returned.
  936.  
  937. In the following routine, the text between <> should appear as one
  938. line without the <> symbol:
  939.    
  940.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  941.    "GetModuleHandle">
  942.    Sub Main
  943.      If IsAppLoaded("Excel") = 0 Then Shell "excel"
  944.      chan = DDEInitiate ("excel", "c:\accts\budget.xls")
  945.      data$ = DDERequest$ (chan, "total")
  946.      If val(data$) = 0 then DDEPoke chan, "total", "$100.00"
  947.      DDETerminate chan
  948.    End Sub
  949.  
  950. DDEExecute
  951. ----------
  952.  
  953. Syntax:
  954.  
  955.    DDEExecute <ChanNum>, <ExecuteString$>
  956.   
  957. DDEExecute is used by a client to execute a command, function, or
  958. macro in the server. The string variable <ExecuteString$> is the
  959. server's command, function, or macro. If the <ExecuteString$> does not
  960. exist, a WordBasic error is generated.
  961.  
  962. When executed, the macro below checks to see if Excel is already
  963. running. If it is not, the macro starts Excel. If you don't know what
  964. sheet(s) is currently open when Excel is loaded, use "system" as the
  965. second parameter of the DDEInitiate command. You can then issue an
  966. Excel OPEN command using DDEExecute to open a specific file.
  967.   
  968. In the following routine, the text between <> should appear as one
  969. line without the <> symbol:
  970.   
  971.    <Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias
  972.    "GetModuleHandle">
  973.    Sub MAIN
  974.      If IsAppLoaded ("EXCEL") = 0 Then Shell "EXCEL.EXE"
  975.      ExecuteString$ = "=B1{enter}"
  976.      ChanNum = DDEInitiate("EXCEL", "C:\WINDOWS\SHEET1.XLS")
  977.      DDEExecute ChanNum, "[App.Restore()][Formula.Goto( " + Chr$(34) +
  978.         "test" + Chr$(34) + ")]"
  979.      DDEExecute ChanNum,  ExecuteString$
  980.      DDEExecute ChanNum, "[App.Minimize()]"
  981.      DDETerminate ChanNum
  982.    End Sub
  983.  
  984. The Formula.Goto statement in the above DDEExecute statement is the
  985. equivalent of the following Excel macro statement:
  986.  
  987.    =FORMULA.GOTO("test")
  988.  
  989.  
  990. FIELD CODES -- DDEAUTO AND DDE
  991. ==============================
  992.  
  993. DDE field codes are best used when you are working with a document
  994. containing information from supporting documents that changes
  995. periodically. To maintain the most current information in the
  996. dependent document, use a DDE field code to link to the supporting
  997. document. DDEAuto will handle the updating automatically for you --
  998. always using the most current version of the source document. The DDE
  999. field code allows you to manually update the information by pressing
  1000. F9.
  1001.  
  1002. You can create these fields three different ways:
  1003.  
  1004. 1. Manually enter the field brackets by pressing CTRL+F9. Type the
  1005.    field type and topic-item information.
  1006.  
  1007. 2. From the Insert menu, choose Fields and select the DDE field types
  1008.    in the Insert Field Type box.
  1009.  
  1010. 3. From the Edit menu, choose Copy and then Paste Link, selecting the
  1011.    Auto Update option if desired.
  1012.  
  1013. Syntax:
  1014.   
  1015.    {DDE [<path>]<appname> <filename> [<reference>]}
  1016.   
  1017.    {DDEAuto [<path>]<appname> <filename> [<reference>]}
  1018.    
  1019. Note: The braces -- { } -- are not typed; they are inserted when you
  1020. choose Field from the Insert menu (or press CTRL+F9).
  1021.  
  1022. If a path is specified, it must include double backslashes ( \\ ). The
  1023. <reference> is optional. The following field statement example is an
  1024. automatic update link to the server BUDGET.XLS file for cells
  1025. R1C1:R4C4. Changing the field name from DDEAUTO to DDE will change the
  1026. updating linkage from automatic to manual.
  1027.   
  1028.    {DDEAuto c:\\apps\\server\\budget.xls R1C1:R4C4}
  1029.  
  1030.  
  1031. INCLUDING EXCEL CHARTS IN WORD FOR WINDOWS DOCUMENTS
  1032. ====================================================
  1033.  
  1034. The easiest way to perform a DDE link from an Excel chart to Word for
  1035. Windows is to do the following:
  1036.  
  1037. 1. In Excel, choose Select Chart from the Chart menu.
  1038.  
  1039. 2. From the Edit menu, choose Copy.
  1040.  
  1041. 3. Switch to Word for Windows (by pressing ALT+TAB).
  1042.  
  1043. 4. Position the insertion point at the point where you want to place
  1044.    the chart.
  1045.  
  1046. 5. From the Edit menu, choose Paste Link.
  1047.  
  1048. 6. In the Paste Link dialog box, select the Auto Update option if you
  1049.    want Word for Windows to automatically update the chart whenever it
  1050.    changes in Excel. If Auto Update is not selected, you must select
  1051.    the chart and recalculate it (by pressing F9) for new charts to
  1052.    appear.
  1053.  
  1054. When the above procedure is followed, Word inserts one of the
  1055. following fields into the document:
  1056.  
  1057.     Auto Update Option   Field Inserted in Document
  1058.     ------------------   --------------------------
  1059.     
  1060.     Selected             {ddeauto Excel Chart1 "" \* mergeformat}
  1061.     Not selected         {dde Excel Chart1 "" \* mergeformat}
  1062.  
  1063. Once the chart is placed in the Word document, the chart can be scaled
  1064. or cropped.
  1065.  
  1066.  
  1067. DISPLAYING DDE LINKED PICTURES
  1068. ==============================
  1069.  
  1070. When you use DDE to link a chart in Excel to a document in Word for
  1071. Windows using the Auto Update feature, the charts in some cases will
  1072. not be identical.
  1073.  
  1074. Because Word for Windows wants the format that will look the best when
  1075. printed, it asks Excel for a different format than is displayed by
  1076. Excel. Therefore, the images will not always match. For example, color
  1077. will not be retained in a chart from Excel that is linked to Word for
  1078. Windows if the printer driver being used does not support color.
  1079.  
  1080. The WIN.INI parameter AskforPrinterPicture= can be used to determine
  1081. how Microsoft Word for Windows will display DDE linked pictures. This
  1082. parameter affects only DDE links and should be entered in the
  1083. [Microsoft Word] section of the WIN.INI file. To modify the WIN.INI,
  1084. use a text editor such as Notepad, or use Word for Windows and save
  1085. the WIN.INI as Text Only.
  1086.  
  1087. The following example allows Word for Windows to display a DDE paste-
  1088. linked picture as it would display when printed:
  1089.  
  1090.    [Microsoft Word]
  1091.    AskforPrinterPicture=1
  1092.    
  1093. If a color printer is installed, then the picture displays in color.
  1094. Otherwise, it displays in black and white.
  1095.  
  1096. The following example allows Word for Windows to display a DDE paste-
  1097. linked picture in color, regardless of the installed printer:
  1098.  
  1099.    [Microsoft Word]
  1100.    AskforPrinterPicture=0
  1101.     
  1102.  
  1103.                    DDE AND Q+E FOR MICROSOFT EXCEL
  1104.                    ===============================
  1105.  
  1106. Q+E can be used with Excel, Word for Windows, or any other application
  1107. that supports DDE. For information on using DDE with Q+E, see the "Q+E
  1108. for Microsoft Excel User's Guide."
  1109.